/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.infra.database.type.dialect;

import org.apache.shardingsphere.infra.database.metadata.dialect.VastbaseDataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.BranchDatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.CommitStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.RollbackStatement;

import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.*;

/**
 * Database type of Vastbase.
 */
public class VastbaseDatabaseType implements BranchDatabaseType {

    private static final Map<String, Collection<String>> SYSTEM_DATABASE_SCHEMA_MAP = new HashMap<>();

    private static final Collection<String> SYSTEM_SCHEMAS = new HashSet<>(Arrays.asList("audit", "blockchain", "cstore", "db4ai", "dbe_perf",
            "dbe_pldebugger", "dbe_pldeveloper", "dbe_sql_util", "dbms_alert", "dbms_application_info", "dbms_assert", "dbms_job", "dbms_lob",
            "dbms_lock", "dbms_pipe", "dbms_profiler_proctable", "dbms_random", "dbms_rowid", "dbms_scheduler", "dbms_session", "dbms_utility", "dbms_xplan",
            "information_schema", "pg_catalog", "pg_toast", "pkg_service", "snapshot", "sqladvisor", "sys", "wmsys", "xmltype", "shardingsphere"));

//    private static final Collection<String> SYSTEM_SCHEMAS = new HashSet<>(Arrays.asList(
//            "information_schema", "sys", "shardingsphere"));

//    private static final Collection<String> SYSTEM_SCHEMAS = Collections.emptySet();

    static {
        SYSTEM_DATABASE_SCHEMA_MAP.put("postgres", SYSTEM_SCHEMAS);
        SYSTEM_DATABASE_SCHEMA_MAP.put("vastbase", SYSTEM_SCHEMAS);
    }

    @Override
    public QuoteCharacter getQuoteCharacter() {
        return QuoteCharacter.QUOTE;
    }

    @Override
    public Collection<String> getJdbcUrlPrefixes() {
        return Collections.singleton(String.format("jdbc:%s:", getType().toLowerCase()));
    }

    @Override
    public VastbaseDataSourceMetaData getDataSourceMetaData(final String url, final String username) {
        return new VastbaseDataSourceMetaData(url);
    }

    @Override
    public void handleRollbackOnly(final boolean rollbackOnly, final SQLStatement statement) throws SQLException {
        ShardingSpherePreconditions.checkState(!rollbackOnly || statement instanceof CommitStatement || statement instanceof RollbackStatement,
                () -> new SQLFeatureNotSupportedException("Current transaction is aborted, commands ignored until end of transaction block."));
    }

    @Override
    public Map<String, Collection<String>> getSystemDatabaseSchemaMap() {
        return SYSTEM_DATABASE_SCHEMA_MAP;
    }

    @Override
    public Collection<String> getSystemSchemas() {
        return SYSTEM_SCHEMAS;
    }

    @Override
    public boolean isSchemaAvailable() {
        return true;
    }

//    @Override
//    public String getDefaultSchema() {
//        return "public";
//    }

    @Override
    public String getType() {
        return "Vastbase";
    }

    @Override
    public DatabaseType getTrunkDatabaseType() {
        return DatabaseTypeFactory.getInstance("MySQL");
    }
}
